package org.thinkinbunny.financial.view;

import org.thinkinbunny.financial.R;
import org.thinkinbunny.financial.data.DbHelper;
import org.thinkinbunny.financial.data.TableSchemaInfo;
import org.thinkinbunny.financial.util.ShowTools;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

public class InputDialog {

	public static Dialog buildDialogForText(final Activity activity,
			final int type) {
		LayoutInflater inflater = LayoutInflater.from(activity);
		final View textEntryView = inflater.inflate(
				R.layout.alert_dialog_text_entry, null);
		final EditText typEditText = (EditText) textEntryView
				.findViewById(R.id.type_edit);
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		// builder.setIcon(R.drawable.alert_dialog_icon);

		// builder.setTitle("参数输入");
		builder.setView(textEntryView);
		builder.setPositiveButton(activity.getString(R.string.add),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int whichButton) {

						if (TextUtils.isEmpty(typEditText.getText().toString()
								.trim())) {
							ShowTools.toast(activity
									.getString(R.string.empty_type));
							return;
						}
						DbHelper dbHelper = new DbHelper(activity,
								DbHelper.DbName);
						SQLiteDatabase database = dbHelper
								.getWritableDatabase();
						String tabelString = "";
						ContentValues contentValues = new ContentValues();
						switch (type) {
						case 1:
							tabelString = TableSchemaInfo.typeTable;
							contentValues.put("type", typEditText.getText()
									.toString().trim());
							break;
						case 2:
							tabelString = TableSchemaInfo.accountTable;
							contentValues.put("account", typEditText.getText()
									.toString().trim());
							break;
						case 3:

							tabelString = TableSchemaInfo.projectTable;
							contentValues.put("project", typEditText.getText()
									.toString().trim());
							break;
						default:
							break;
						}

						if (database.insert(tabelString, null, contentValues) == -1) {
							ShowTools.toastInThread(activity
									.getString(R.string.add_error)
									+ "code=0x001");
						} else {
							ShowTools.toastInThread(activity
									.getString(R.string.add_ok));
						}
						database.close();
						database = null;
					}
				});
		builder.setCancelable(false);
		builder.setNegativeButton(activity.getString(R.string.cancel),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int whichButton) {

					}
				});
		return builder.create();
	}
}
